/**
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.comm.listener.impl;

import java.util.Collection;
import java.util.Observable;

import org.kopsox.comm.listener.Observed;
import org.kopsox.comm.listener.Observer;
import org.kopsox.comm.message.ValueMessage;

/**
 * This Abstract-Class can be used for Observed which should be compatible with java.util.Observeable<br>
 * <b>NOTE</b>: Each Observer which you want to add or remove, has to be from the Type AbstractJDKObserver!<br>
 * 
 * @author Konrad Renner - 22.10.2010
 *
 */
public class AbstractJDKObserved extends Observable implements Observed {
	
	public AbstractJDKObserved() {
		super();
	}

	/* (non-Javadoc)
	 * @see org.kopsox.comm.listener.Observed#addAllObservers(java.util.Collection)
	 */
	@Override
	public boolean addAllObservers(Collection<Observer> observer) {
		if(observer==null || observer.size() == 0) {
			return false;
		}
		
		for(Observer obs:observer) {
			if(obs instanceof AbstractJDKObserver) {
				super.addObserver((AbstractJDKObserver)obs);
			}else {
				throw new IllegalArgumentException("Observer must be from Type AbstractJDKObserver");
			}
		}
		
		return true;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.comm.listener.Observed#addObserver(org.kopsox.comm.listener.Observer)
	 */
	@Override
	public boolean addObserver(Observer observer) {
		if(observer instanceof AbstractJDKObserver) {
			super.addObserver((AbstractJDKObserver)observer);
			
			return true;
		}

		throw new IllegalArgumentException("Observer must be from Type AbstractJDKObserver");
	}

	/**
	 * This method is not avaiable in this context!
	 * 
	 * @return null
	 */
	@Override
	public Collection<Observer> getAllObservers() {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.comm.listener.Observed#notifyObservers(org.kopsox.comm.message.ValueMessage)
	 */
	@Override
	public void notifyObservers(ValueMessage message) {
		super.notifyObservers(message.getMessageValue().getValue());
	}

	/* (non-Javadoc)
	 * @see org.kopsox.comm.listener.Observed#removeObserver(org.kopsox.comm.listener.Observer)
	 */
	@Override
	public boolean removeObserver(Observer observer) {
		if(observer instanceof AbstractJDKObserver) {
			super.deleteObserver((AbstractJDKObserver)observer);
			
			return true;
		}

		throw new IllegalArgumentException("Observer must be from Type AbstractJDKObserver");
	}

}
